<template>
    <div class="app-container">

        <el-row :gutter="20">
            <!--科室数据-->
            <el-col :span="3" :xs="20" style="width: 11%;border-right-style: solid;border-width: 1px">
                <div class="head-container">
                    <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick"/>
                </div>
            </el-col>
            <!--数据-->
            <el-col :span="20" :xs="24" style="width: 89%">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                    <el-form-item label="学生姓名" prop="nickName">
                        <el-input v-model="queryParams.nickName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery"/>
                    </el-form-item>
                    <el-form-item label="考试" prop="examId">
                        <el-select v-model="queryParams.examId" filterable remote reserve-keyword placeholder="请选择考试" clearable @keyup.enter.native="handleQuery"
                                   :remote-method="findExamByName" @change="queryExamIdChange" :loading="find_ex_loading">
                            <el-option v-for="item in exams" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="考试时间" prop="examTime">
                        <el-date-picker v-if="queryParams.examId == null || queryParams.examId == ''" v-model="queryParams.params.examTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择考试时间"></el-date-picker>
                        <el-select v-else v-model="queryParams.params.examTime" placeholder="请选择考试时间" clearable>
                            <el-option v-for="item in examTimes" :key="item" :label="item" :value="item"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="得分" prop="score">
                        <InputNumberRange size="small" controls_position="right" :value="scoreRange"/>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ex:ex_record:export']">导出成绩单</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
                </el-row>

                <el-table v-loading="loading" :data="ex_recordList" @selection-change="handleSelectionChange" @sort-change="sortChange" :height="table_height">
                    <el-table-column type="selection" width="55" align="center"/>
                    <el-table-column label="学生姓名" align="center" prop="nickName"/>
                    <el-table-column label="考试" align="center" prop="exam.name"/>
                    <el-table-column label="总题数" align="center" prop="exam.questionCount"/>
                    <el-table-column label="对题数" align="center" prop="rightCount"/>
                    <el-table-column label="得分" align="center" prop="score" sortable="custom">
                        <template slot-scope="scope">
                            <span v-html="scoreFormat(scope.row.score)"></span>
                        </template>
                    </el-table-column>
                    <el-table-column label="答题用时" align="center" prop="answerTime" :formatter="second_format"/>
                    <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" align="center" prop="status" width="90">
                        <template slot-scope="scope">
                            <dict-tag :options="dict.type.ex_exam_record_status_dict" :value="scope.row.status"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button v-if="scope.row.status == '3'" size="mini" type="text" icon="el-icon-view" @click="checkExam(scope.row)" v-hasPermi="['ex:ex_record:view']">查看</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {listEx_record, delEx_record} from "@/api/ex/ex_record";
import {listExam} from "@/api/ex/exam";
import {secondFormat, getRangeDay, nowDateStr} from "@/utils/zzDouUtils/dateUtil";
import {isEmptyArr} from "@/utils/zzDouUtils/arrayUtil";
import InputNumberRange from "@/views/components/number/input-number-range.vue";
import {parseTime} from "@/utils/ruoyi";
import {deptTreeSelect} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";

export default {
    name: "Ex_record",
    dicts: ['ex_exam_record_status_dict'],
    components:{InputNumberRange,Treeselect},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 考试记录表格数据
            ex_recordList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userId: null,
                nickName: null,
                deptId: null,
                examId: null,
                examTime: null,
                questionCount: null,
                rightCount: null,
                score: null,
                answerTime: null,
                startTime: null,
                endTime: null,
                status: null,
                reservedOne: null,
                reservedTwo: null,
                params :{},
            },
            scoreRange:[],
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            //搜索考试加载
            find_ex_loading: false,
            //搜索考试列表
            exams: [],
            examTimes: [],
            // 科室树选项
            deptOptions: undefined,
            defaultProps: {
                children: "children",
                label: "label"
            },
        };
    },
    created() {
        this.getList();
        this.getDeptTree();
    },
    methods: {
        /** 查询科室下拉树结构 */
        getDeptTree() {
            deptTreeSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 科室节点单击事件
        handleNodeClick(data) {
            this.queryParams.params.deptId = data.id;
            this.handleQuery();
        },
        /** 格式化分数 */
        scoreFormat(cellValue){
            if (cellValue === null){
                cellValue = 0;
            }
            let color = (cellValue >= 80) ? '#67C23A' : (cellValue < 80 && cellValue >= 50) ? '#E6A23C' : '#F56C6C';
            return `<span style="color: ${color};font-size: 18px;font-weight: bold;">${cellValue}</span>`
        },
        second_format(row, column,cellValue) {
            return secondFormat(cellValue);
        },
        /** 排序改变事件 */
        sortChange(column){
            this.queryParams.params.scoreOrder = (column.order == 'ascending') ? 'ASC' : 'DESC';
            this.getList()
        },
        /** 查询考试记录列表 */
        getList() {
            this.loading = true;
            if (!isEmptyArr(this.scoreRange)){
                this.queryParams['beginScore'] = this.scoreRange[0];
                this.queryParams['endScore'] = this.scoreRange[1];
            }
            listEx_record(this.queryParams).then(response => {
                this.ex_recordList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                userId: null,
                examId: null,
                questionCount: null,
                rightCount: null,
                score: null,
                answerTime: null,
                startTime: null,
                endTime: null,
                status: null,
                reservedOne: null,
                reservedTwo: null,
                createTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.params = {};
            this.scoreRange = [];
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除考试记录编号为"' + ids + '"的数据项？').then(function () {
                return delEx_record(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('ex/ex_record/export', {
                ...this.queryParams
            }, `成绩单_${nowDateStr()}.xlsx`)
        },
        /** 查看试卷 */
        checkExam(row){
            this.$tab.openPage(row.exam.name, '/ex/check/index.vue/'+ row.id +'/' + 1, {});
        },
        //---------------------
        /** 根据名称查询考试 */
        findExamByName(query){
            if (query !== '') {
                this.find_ex_loading = true;
                listExam({pageSize: 100,pageNum: 1,name:query}).then(response=>{
                    this.exams = response.rows;
                    this.find_ex_loading = false;
                })
            } else {
                this.exams = [];
            }
        },
        /** 选中考试事件 */
        queryExamIdChange(item){
            this.exams.forEach( exam =>{
                if (exam.id === item){
                    this.examTimes = getRangeDay(exam.startTime,exam.endTime);
                }
            })
        }
    },
};
</script>
